package com.sample;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class MultiplyView extends View {

	private Paint rectangleOnePaint;
	private Paint rectangleTwoPaint;
	private Paint textPaint;
	private Paint bgrndPaint;

	private String textOne;
	private String textTwo;
	private int textHeight;

	public MultiplyView(Context context) {
		super(context);
		initMultiplyView();
	}

	public MultiplyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initMultiplyView();
	}

	public MultiplyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initMultiplyView();
	}

	private void initMultiplyView() {
		setFocusable(true);
		Resources r = this.getResources();

		rectangleOnePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		rectangleOnePaint.setColor(r.getColor(R.color.colorGreen));
		rectangleOnePaint.setStrokeWidth(1.5f);
		rectangleOnePaint.setStyle(Paint.Style.STROKE);

		rectangleTwoPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		rectangleTwoPaint.setColor(r.getColor(R.color.colorRed));
		rectangleTwoPaint.setStrokeWidth(1.5f);
		rectangleTwoPaint.setStyle(Paint.Style.STROKE);
		
		bgrndPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		bgrndPaint.setColor(r.getColor(R.color.backgroundColor));
		bgrndPaint.setStrokeWidth(0f);
		bgrndPaint.setStyle(Paint.Style.FILL_AND_STROKE);


		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(r.getColor(R.color.textColor));
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setStrokeWidth(1.0f);

		textHeight = (int) textPaint.measureText("yY");

		textOne = r.getString(R.string.textOne);
		textTwo = r.getString(R.string.textTwo);

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int sizeX = measure(widthMeasureSpec);
		int sizeY = measure(heightMeasureSpec);

		int dimension = Math.min(sizeX, sizeY);
		setMeasuredDimension(dimension, dimension);

	}

	private int measure(int widthMeasureSpec) {
		int mode = MeasureSpec.getMode(widthMeasureSpec);
		int size = MeasureSpec.getSize(widthMeasureSpec);

		if (mode == MeasureSpec.UNSPECIFIED)
			return 100;
		else
			return size;

	}

	@Override
	protected void onDraw(Canvas canvas) {
		int mWidth = getMeasuredWidth();
		int mHeight = getMeasuredHeight();

		int px = getMeasuredWidth() / 2;
		int py = getMeasuredHeight() / 2;
		int radius = (int) (Math.min(px, py) / 1.44f);

		int cardinalX = px - (int) (textPaint.measureText(textOne) / 2l);
		int cardinalY = py - (textHeight);
		canvas.rotate(45,px,py);
		Rect r1 = new Rect(px - radius, py - (2 * textHeight), px + radius, py
				+ (2 * textHeight));
		canvas.drawRect(r1, rectangleOnePaint);

		canvas.drawText(textOne, cardinalX, r1.exactCenterY()
				+ (textHeight / 2l), textPaint);

		// canvas.save();
		canvas.rotate(90, px, py);

		canvas.drawRect(r1, rectangleTwoPaint);
		cardinalX = px - (int) (textPaint.measureText(textTwo) / 2l)
				- (textHeight / 2);

		canvas.drawText(textTwo.substring(0, (int) (textTwo.length() - 1) / 2),
				cardinalX, r1.exactCenterY() + (textHeight / 2l), textPaint);
		canvas.drawText(
				textTwo.substring((int) (textTwo.length() - 1) / 2 ),
				cardinalX
						+ textPaint.measureText(textTwo.substring(0,
								(int) (textTwo.length() - 1) / 2))
						+ (textPaint.measureText("W")), r1.exactCenterY()
						+ (textHeight / 2l), textPaint);
		canvas.rotate(45,px,py);
		canvas.drawText(".", px, py, bgrndPaint);
		// canvas.restore();

	}
}
